package com.ruoyi.system.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.CommonConstant;
import com.ruoyi.common.core.domain.entity.admin.AdminRichTextDB;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.global.CustomException;
import com.ruoyi.common.utils.sql.MyPageUtils;
import com.ruoyi.system.mapper.admin.AdminRichTextMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class AdminRichTextService {

    @Resource
    private AdminRichTextMapper adminRichTextMapper;


    public TableDataInfo selectAdminRichTextList(AdminRichTextDB adminRichText) {
        LambdaQueryWrapper<AdminRichTextDB> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotEmpty(adminRichText.getId()), AdminRichTextDB::getId, adminRichText.getId());
        wrapper.like(ObjectUtils.isNotEmpty(adminRichText.getTitle()), AdminRichTextDB::getTitle, adminRichText.getTitle());
        wrapper.like(ObjectUtils.isNotEmpty(adminRichText.getRichContent()), AdminRichTextDB::getRichContent, adminRichText.getRichContent());

        wrapper.eq(ObjectUtils.isNotEmpty(adminRichText.getIsSkip()), AdminRichTextDB::getIsSkip, adminRichText.getIsSkip());
        wrapper.eq(ObjectUtils.isNotEmpty(adminRichText.getIsMark()), AdminRichTextDB::getIsMark, adminRichText.getIsMark());

        // 创建时间筛选
        if (ObjectUtils.isNotEmpty(adminRichText.getCreatedStart()) && ObjectUtils.isNotEmpty(adminRichText.getCreatedEnd())) {
            wrapper.between(AdminRichTextDB::getCreatedAt, adminRichText.getCreatedStart(), adminRichText.getCreatedEnd());
        }
        // 修改时间筛选
        if (ObjectUtils.isNotEmpty(adminRichText.getUpdatedStart()) && ObjectUtils.isNotEmpty(adminRichText.getUpdatedEnd())) {
            wrapper.between(AdminRichTextDB::getUpdatedAt, adminRichText.getUpdatedStart(), adminRichText.getUpdatedEnd());
        }

        wrapper.orderByDesc(AdminRichTextDB::getId);
        Page<AdminRichTextDB> pageParam = MyPageUtils.handlePageParam();
        Page<AdminRichTextDB> adminPlotDBPage = adminRichTextMapper.selectPage(pageParam, wrapper);
        return TableDataInfo.covert(adminPlotDBPage);
    }

    public List<AdminRichTextDB> getDetailListByLastId(Long id) {
        LambdaQueryWrapper<AdminRichTextDB> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRichTextDB::getIsActive, CommonConstant.CONSTANT_NUM.ONE);
        queryWrapper.orderByDesc(AdminRichTextDB::getIsMark);
        queryWrapper.orderByDesc(AdminRichTextDB::getId);

        if (ObjectUtils.isNotEmpty(id)) {
            // 小于id
            queryWrapper.lt(AdminRichTextDB::getId, id);
        }
        // 最大10条
        queryWrapper.last("limit 10");
        List<AdminRichTextDB> adminRichTextDBS = adminRichTextMapper.selectList(queryWrapper);
        // 使用html解析处文本

        for (AdminRichTextDB adminRichTextDB : adminRichTextDBS) {
            getTextContentByRichText(adminRichTextDB);
        }
        return adminRichTextDBS;
    }

    public List<AdminRichTextDB> selectAdminRichTextByIds(List<Long> ids) {
        return adminRichTextMapper.selectBatchIds(ids);
    }

    public List<AdminRichTextDB> appSelectRichTextList(AdminRichTextDB adminRichText) {
//        if (){
//
//        }
        return null;

    }

    public int insertAdminRichText(AdminRichTextDB adminRichText) {
        return adminRichTextMapper.insert(adminRichText);
    }

    public int updateAdminRichText(AdminRichTextDB adminRichText) {
        return adminRichTextMapper.updateById(adminRichText);
    }

    public static String extractText(String html) {
        HTMLEditorKit kit = new HTMLEditorKit();
        HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
        doc.putProperty("IgnoreCharsetDirective", true);
        try {
            kit.read(new StringReader(html), doc, 0);

            return doc.getText(0, doc.getLength()).trim();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (BadLocationException e) {
            throw new RuntimeException(e);
        }
    }

    public AdminRichTextDB selectAdminRichTextById(Long id) {
        AdminRichTextDB adminRichTextDB = adminRichTextMapper.selectById(id);
        if (Objects.equals(adminRichTextDB.getIsDeleted(), CommonConstant.CONSTANT_NUM.ONE)){
            return AdminRichTextDB.builder().build();
        }
        getTextContentByRichText(adminRichTextDB);
        return adminRichTextDB;
    }

    private static void getTextContentByRichText(AdminRichTextDB adminRichTextDB) {
        try {
            String text = extractText(adminRichTextDB.getRichContent());
            adminRichTextDB.setTextContent(text);
        } catch (Exception e) {
            throw new CustomException("解析html异常");
        }
    }
}
